/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.odb.bzk3.midp;

import javax.microedition.m3g.Appearance;
import javax.microedition.m3g.Mesh;
import javax.microedition.m3g.PolygonMode;
import javax.microedition.m3g.TriangleStripArray;
import javax.microedition.m3g.VertexArray;
import javax.microedition.m3g.VertexBuffer;

/**
 * 
 * @author daniel
 */
public class TriangleFactory {
	static Mesh makeTrig(float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3, int aColor) {
		return makeTrig(
				(short) ((x1 / 255.0f) * (1 << 16)),
				(short) ((y1 / 255.0f) * (1 << 16)),
				(short) ((z1 / 255.0f) * (1 << 16)),
				(short) ((x2 / 255.0f) * (1 << 16)),
				(short) ((y2 / 255.0f) * (1 << 16)),
				(short) ((z2 / 255.0f) * (1 << 16)),
				(short) ((x3 / 255.0f) * (1 << 16)),
				(short) ((y3 / 255.0f) * (1 << 16)),
				(short) ((z3 / 255.0f) * (1 << 16)), 
				aColor);
	}
	
	public static Mesh makeTrig(short x1, short y1, short z1, short x2,
			short y2, short z2, short x3, short y3, short z3, int aColor) {
		VertexArray va = new VertexArray(3, 3, 2);
		{
			short[] values = { x1, y1, z1, x2, y2, z2, x3, y3, z3 };
			va.set(0, 3, values);
		}
		VertexBuffer vb = new VertexBuffer();
		vb.setDefaultColor(aColor);
		vb.setPositions(va, 255.0f / (1 << 16), null);
		PolygonMode pm = new PolygonMode();
		pm.setCulling(PolygonMode.CULL_NONE);
		pm.setWinding(PolygonMode.WINDING_CCW);
		Appearance ap = new Appearance();
		ap.setPolygonMode(pm);

		int[] strip = { 0, 1, 2 };
		int[] lengths = { 3 };
		TriangleStripArray tsa = new TriangleStripArray(strip, lengths);

		return new Mesh(vb, tsa, ap);
	}

}
